package com.hzhr.iot.user.controller;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.Author;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.user.controller.base.BaseController;
import com.hzhr.iot.user.model.dept_parentId.Dept_ParentIdGetModel;
import com.hzhr.iot.user.model.dept_parentId.Dept_ParentIdPutModel;
import com.hzhr.iot.user.service.Dept_ParentIdService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(tags = "Dept_ParentId", description = "部门的上级部门编号")
@RestController
@RequestMapping(value = "")
public class Dept_ParentIdController extends BaseController {
    @Autowired
    Dept_ParentIdService dept_parentIdService;

    //region /depts/{deptId}/dept-ids PUT
    //region swagger响应码 : putByDeptId
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : putByDeptId
    @ApiOperation(value="管理员更新部门的上级编号", notes="管理员更新部门的上级编号  \n简介 : 更新部门的上级部门编号  \n步骤 :  \n1. 查询部门 : (√)  \n 根据路径参数deptId查询登录人的管辖范围下的部门(DEPT)dept_1，如果dept_1不存在，抛出异常201；  \n2. 查询上级部门 : (√)  \n 根据body.parentId查询登录人的管辖范围下的部门(DEPT)dept_2，如果dept_2不存在，抛出异常202；  \n3. 验证部门是否具备更新上级部门编号的条件 : (√)  \n 条件1：dept_1.name在dept_2的直接下级部门中不能存在，如果存在，抛出异常203；  \n 条件2：dept_1不能存在下级部门或下级用户，如果存在，抛出异常204；  \n4. 更新部门信息 : (√)  \n 根据body设置dept_1的相应属性，并且同时设置以下属性：  \n (1) dept_1.parentName = dept_2.name；  \n (2) dept_1.code的设置规则：  \n 如果dept_2不存在直接下级部门，则设置为 dept_2.code + '01'；  \n 如果dept_2存在直接下级部门，则查询出其直接下级部门的最大code，提取出该code的最后两位，转换为数字并在此基础上 +1 ，  \n 然后左补零至两位字符串str_1，最后将dept_1.code设置为 dept_2.code + str_1；  \n (3) dept_1.resourceCode = dept_2.code + “dept_1.code(上一步生成)的最后两位”；  \n5. 返回信息 : (√)  \n 根据条件参数查询更新结果，组装响应并返回；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : putByDeptId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "deptId", value = "部门编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })

    //endregion
    //region 请求 : putByDeptId
    @RequestMapping(value = "/depts/{deptId}/dept-ids", method = RequestMethod.PUT)
    //endregion
    @Author
    public ResponseEntity<Dept_ParentIdGetModel> putByDeptId(@PathVariable("deptId") Integer deptId,
                                                             @RequestParam @ApiIgnore Map<String, Object> query,
                                                             @RequestBody Dept_ParentIdPutModel body) throws ServiceException {
        Dept_ParentIdGetModel responseBody = dept_parentIdService.putByDeptId(initHeader(), deptId, query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
        //endregion
    }
    //endregion
}
















































































































































































































































































































































































